package com.example.practical_training_projects.controller;

import com.example.practical_training_projects.entity.pc;
import com.example.practical_training_projects.entity.questionform;
import com.example.practical_training_projects.entity.user;
import com.example.practical_training_projects.service.questionformService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin //允许跨域执行
@RequestMapping("questionform")
public class questionformController {
    @Autowired
    private questionformService service;

    /**
     * GET http://localhost:8080/questionform/list 显示
     * POST http://localhost:8080/questionform/add 添加
     * GET http://localhost:8080/questionform/del/{{q_uid}} 删除
     * POST http://localhost:8080/questionform/update 更新
     * GET http://localhost:8080/questionform/get/{{q_uid}} 获取
     * GET http://localhost:8080/questionform/query/{{q_name}} 查询
     *
     *
     * @return
     */

    @GetMapping("list")
    public List<questionform> getAllQF() {
        return service.getAllQF();
    }
    @GetMapping("/page")
    public PageInfo<questionform> getAllpage(Integer pageNum, Integer pageSize){
        PageInfo<questionform> pageInfo = service.findqfpage(pageNum,pageSize);
        return pageInfo;
    }

    @PostMapping("add")
    public Integer insertQF(@RequestBody questionform qf) {
        //@RequestBody能接收转换的字符串，并匹配到StudentInfo studentInfo中
        return service.insertQF(qf);
    }

    @GetMapping("/del/{q_uid}")
    public Integer deleteQF(@PathVariable("q_uid") Integer qf) {
        return service.deleteQF(qf);
    }

    @PostMapping("update")
    public Integer updateQF(@RequestBody questionform qf) {
        return service.updateQF(qf);
    }

    @GetMapping("/query")
    public ResponseEntity<List<questionform>> getQuestionsByMultiCriteria(
            @RequestParam(value = "search", required = false) String search) {
        System.out.println("Received search param: " + search);
        try {
            List<questionform> questions = service.getQuestionsByMultiCriteria(search);
            if (questions.isEmpty()) {
                return new ResponseEntity<>(HttpStatus.NO_CONTENT); // 无内容时返回204状态码
            }
            return new ResponseEntity<>(questions, HttpStatus.OK); // 有内容时返回200状态码和数据
        } catch (Exception e) {
            return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); // 出错时返回500状态码
        }
    }

    @GetMapping("/get/{q_uid}")
    public questionform findQFuid(@PathVariable("q_uid") Integer q_uid) {
        return service.findQFuid(q_uid);
    }


    @PostMapping("/like/{q_uid}")
    public ResponseEntity<?> likeQuestionForm(@PathVariable("q_uid") Integer q_uid) {
        service.incrementLikes(q_uid);
        questionform updatedQf = service.findQFuid(q_uid);
        return ResponseEntity.ok(updatedQf.getQ_dianzan());
    }
}

